package com.specialized.pay.Payutils;

import com.alipay.api.AlipayConfig;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author：wangxinyu
 * @Package：com.specialized.pay.Payutils
 * @Project：Specialized-Cloud
 * @name：AlipayUtils
 * @Date：2024/7/25 15:31
 */
@Component
public class AlipayUtils {

    public static final String NOTIFY_URL="https://49.235.148.10:9215/pay/notify_url";
    public static final String RETURN_URL="http://49.235.148.10:9215/pay/notify_url";



    /**
     * 配置sdk
     * @return
     */
    public static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDk2eJJuhqmyI12IYT5CQXRzsjFQuO/uo6UFXqjt4nBmMmDgjytU0ZYmb+RGl5T53qYpHALXYfy8ssBDuqaz+byFrZzPOVp2vg4vSk/loAuhjG+jsXnsoM/3ogqBYsYm3q8hrr1sK+zsBvMeOFeHuZ0hgiFOGc+4V8bIYZ3ME9oZxPJZxrUmQIyzKHG0SlUz/EAsqQAVzY7iQmF8uXVLDniUZRYadGdOgsvDVCw/aTDPUlM28v69eaJfXF9SnVpFUiDCm52f2JzqGwhnbY4V6w+ibmmteMjsVxKWLzaCqsjriQJLiJPlMvCUKD6iKeF8xHp7OrCDA2+6iF3ttCs8miZAgMBAAECggEAJPrIRB2cfOLfaP1VnWBF3ndJngaHAs5Eh81IuffIfM4RkfyTjXZtYZfGis31/BzlhJDdwhhlBW8SOqOMaWO679eXt9tZPtexUInQhoPtRpdMpnwvopcH+mQdIJ0wstM87AXhZB4e5ixI8at+z1Sy7JVT/yL0HAcS9TFfpxbZXgcrra0fcTscDBcWf5XlS2GsP3mF4f8oaD73BBRpd2rBGMT4cKShxbvtTb0/zfQJmdlTDU1njz6wiPVQmz3GRs19rJPdlQL5AqrjqdnxBQzEGtQrLp+NVHTd42xRnCe0clhQS/RMG/cQBEYYvf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m6TCfJXS9kojtnAo9uSEdzGu181MvXqS62CjUuEybSXWhYvPPLlCSJTf/7hIadYoz9AEsMK0d/TUOrfh7Ai8flXMOjBpou83LD5lppcbH+5VcO1AUrw3nvBGm0fQ1FWbaRcM3NpAbmY8zR7GZi6LWENthCtjUMtEMctmQXlQdinbx067ws1RqY24nzg7ZG4HgBQxVGZKMYCMhdODSPWyVFB3CjJXQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000139637917");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

    /**
     * 验签
     * @param request
     * @return
     */

    public static Map<String, String> getStringStringMap(HttpServletRequest request) {
        //获取异步传来的数据
        Map<String, String> map = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            try {
                valueStr = new String(valueStr.getBytes(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            map.put(name, valueStr);
        }
        System.out.println("支付结果---：" + map.toString());
        return map;
    }

}
